package com.avcdata.aoscustommade.model;

import android.content.Context;

import com.avcdata.aoscustommade.bean.LocatityCacheData;
import com.avcdata.aoscustommade.bean.TopBrandAndSelfBrandBean;
import com.avcdata.aoscustommade.main.BaseCallBack;
import com.avcdata.aoscustommade.utils.AppUtils;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.http.AvcHttpUtils;
import com.avcdata.aoscustommade.utils.http.AvcJsonHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 主页的model
 * Created by zyh on 17/6/7.
 */

public class MainActivityModel {

    private final Context mContext;

    public MainActivityModel(Context context) {
        this.mContext = context;
    }

    public void loadBrandList(String category, int topBrandCount, int lastWeekCount, String lastWeek, String bigRegion, String officeName, String cityName, String brand, String secreKey, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("category", category);
            jsonObject.put("topBrandCount", topBrandCount);
            jsonObject.put("lastWeekCount", lastWeekCount);
            jsonObject.put("lastWeek", lastWeek);
            jsonObject.put("bigRegion", bigRegion);
            jsonObject.put("officeName", officeName);
            jsonObject.put("cityName", cityName);
            jsonObject.put("brand", brand);
            jsonObject.put("secretKey", secreKey);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        AvcHttpUtils.post(AvcConstants.URLHeaderBase+AvcConstants.URLHeaderNation1 + "topBrandAndSelfBrand", jsonObject, true, new AvcJsonHttpResponseHandler(mContext, TopBrandAndSelfBrandBean.class) {

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                TopBrandAndSelfBrandBean bean = (TopBrandAndSelfBrandBean) object;
                baseCallBack.onSuccess(bean);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }

    /**
     * 全国走势图
     * @param module_typeString
     * @param lastWeek
     * @param baseCallBack
     */
    public void loadNationalTrend(String module_typeString, String brand, int topBrandCount, int lastWeekCount, String lastWeek, String bigRegion, String officeName, String cityName, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("category", module_typeString);
            jsonObject.put("brand", brand);
            jsonObject.put("topBrandCount", topBrandCount);
            jsonObject.put("lastWeekCount", lastWeekCount);
            jsonObject.put("lastWeek", lastWeek);
            jsonObject.put("bigRegion", bigRegion);
            jsonObject.put("officeName", officeName);
            jsonObject.put("cityName", cityName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AvcHttpUtils.post(AvcConstants.URLHeaderBase+AvcConstants.URLHeaderNation1 + "trend", jsonObject, true, new AvcJsonHttpResponseHandler(mContext) {


            @Override
            public void onSuccess(JSONObject jsonObject) {
                super.onSuccess(jsonObject);
                baseCallBack.onSuccess(jsonObject);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }

    /**
     * 行业销售额同环比
     * @param lastWeek
     * @param baseCallBack
     */
    public void loadIndustrySales(String lastWeek, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("lastWeek", lastWeek);
            jsonObject.put("bigRegion", "all");
            jsonObject.put("officeName", "all");
            jsonObject.put("cityName", "all");
            jsonObject.put("brand", "A.O.史密斯");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AvcHttpUtils.post(AvcConstants.URLHeaderBase+AvcConstants.URLHeaderNation1 + "brandAmountRatio", jsonObject, true, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject jsonObject) {
                super.onSuccess(jsonObject);
                baseCallBack.onSuccess(jsonObject);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }


        });
    }

    /**
     * 行业市占饼图
     * @param lastWeek
     * @param baseCallBack
     */
    public void loadPiePicture(String lastWeek, final BaseCallBack baseCallBack) {
        ArrayList<String> productDataString = new ArrayList<>();
        productDataString.add("热水器");
        productDataString.add("燃气热水器");
        productDataString.add("电储水热水器");
        productDataString.add("净化器");
        productDataString.add("净水器");
        LocatityCacheData.productData = productDataString;
        JSONObject jsonObject = new JSONObject();
        try {
            JSONArray array = new JSONArray();
            for (int i = 0; i < productDataString.size(); i++) {
                array.put(productDataString.get(i));
            }
            jsonObject.put("categories", array);
            jsonObject.put("topBrandCount", 5);
            jsonObject.put("lastWeek", lastWeek);
            jsonObject.put("lastWeekCount", "0");
            jsonObject.put("brand", "A.O.史密斯");
            jsonObject.put("bigRegion", "all");
            jsonObject.put("officeName", "all");
            jsonObject.put("cityName", "all");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        AvcHttpUtils.post(AvcConstants.URLHeaderBase+AvcConstants.URLHeaderNation1 + "marketShare", jsonObject, true, new AvcJsonHttpResponseHandler(mContext) {

            @Override
            public void onSuccess(JSONObject jsonObject) {
                super.onSuccess(jsonObject);
                baseCallBack.onSuccess(jsonObject);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(Throwable throwable) {
                super.onFailure(throwable);
                baseCallBack.onFailure(throwable);
            }
        });
    }
}
